tools/ocaml/libs: Fix memory/resource leaks with caml_alloc_custom()
authorAndrew Cooper <andrew.cooper3@citrix.com>
Wed, 1 Feb 2023 11:27:42 +0000 (11:27 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 9 Feb 2023 15:55:25 +0000 (15:55 +0000)
commit587823eca162d063027faf1826ec3544f0a06e78
tree62e6bb8bfd6c9aa55e11c8feb4a26b66d28b220e
parentbf935b1ff7cc76b2d25f877e56a359afaafcac1f
tools/ocaml/libs: Fix memory/resource leaks with caml_alloc_custom()

All caml_alloc_*() functions can throw exceptions, and longjump out of
context.  If this happens, we leak the xch/xce handle.

Reorder the logic to allocate the the Ocaml object first.

Fixes: 8b3c06a3e545 ("tools/ocaml/xenctrl: OCaml 5 support, fix use-after-free")
Fixes: 22d5affdf0ce ("tools/ocaml/evtchn: OCaml 5 support, fix potential resource leak")
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Christian Lindig <christian.lindig@citrix.com>
(cherry picked from commit d69ccf52ad467ccc22029172a8e61dc621187889)
tools/ocaml/libs/eventchn/xeneventchn_stubs.c